<?
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Promotion extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();
		
		//load helper
		$this->load->helper('login_helper');
		
			//load model

		$this->load->model('Mpromotion');
		//session_start();
		$la =$this->session->userdata("lang");
		//$data["lang"]= $la;
	

    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
			
	// check login
	check_login();
	
	load_lang("admin");
	}
	

	function index($page='')
	{
	
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/promotion/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mpromotion->count_all();

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Mpromotion->list_data($page,$limit);// $this->Muser->user_review($page, $limit);

    $data['title'] = $this->lang->line('title_promotion_review');

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'promotion_review';

    $this->load->view('admin/template_main', $data);
	}
	
	
	
	//***********update  *************//
		function update()
	{
		$id= $this->uri->segment(4);
	 if ($this->input->post('update'))

    {
	
		
		  //set rules

  			 $this->form_validation->set_rules('title_en', 'English Name', 'required');

      $this->form_validation->set_rules('title_vi', 'Vietnamese Name', 'required');

  //   $this->form_validation->set_rules('title_cn', 'Chinese Name', 'required');

      $this->form_validation->set_rules('detailed_content_en', 'English detail', 'required');

      $this->form_validation->set_rules('detailed_content_vi', 'Vietnamese detail', 'required');

    // $this->form_validation->set_rules('detailed_content_cn', 'Chinese detail', 'required');

    
   
		  if ($this->form_validation->run() && $this->update_proc())

        redirect('admin/promotion/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/promotion/', 'refresh');

		}
		
			 //reload template

		
		$data['result']= $this->Mpromotion->info_from_id($id);
		  $data['title'] = $this->lang->line('title_promotion_update');
		    $data['main'] = 'promotion_update';
			  $this->load->view('admin/template_main', $data);
		
	}
	function update_proc()
	{
		$id= $this->uri->segment(4);
		if(isset($_POST["update"]))
		{
		
			$info= array("vi" => htmlentities($this->input->post("detailed_content_vi"), ENT_QUOTES, 'UTF-8'),
		//	"cn" => htmlentities($this->input->post("detailed_content_cn"), ENT_QUOTES, 'UTF-8'),
			"en" => htmlentities($this->input->post("detailed_content_en"), ENT_QUOTES, 'UTF-8'),);
			$name = array("vi" => htmlentities($this->input->post("title_vi"), ENT_QUOTES, 'UTF-8'),
		//	"cn" => htmlentities($this->input->post("title_cn"), ENT_QUOTES, 'UTF-8'),
			"en" => htmlentities($this->input->post("title_en"), ENT_QUOTES, 'UTF-8'),);

		
		$data = array(
			"name" =>serialize($name),
			"content" => serialize($info),
		
			
			);
	
	
			
		if($this->Mpromotion->update($data,$id))
			return true;
		else
			return false;
		}
		else
			return false;
		
	
	
	}
	
	
	//*********** Create  *************//
	function create()
	{
	 if ($this->input->post('create'))

    {
	

      //set rules

    $this->form_validation->set_rules('title_en', 'English Name', 'required');

      $this->form_validation->set_rules('title_vi', 'Vietnamese Name', 'required');

   //  $this->form_validation->set_rules('title_cn', 'Chinese Name', 'required');

      $this->form_validation->set_rules('detailed_content_en', 'English detail', 'required');

      $this->form_validation->set_rules('detailed_content_vi', 'Vietnamese detail', 'required');

     //$this->form_validation->set_rules('detailed_content_cn', 'Chinese detail', 'required');




      if ($this->form_validation->run() && $this->promotion_create_proc())

        redirect('admin/promotion/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/promotion/', 'refresh');

		}



	
		$data['main'] = 'promotion_create';

    $data['title'] = $this->lang->line('title_promotion_create');

		$this->load->view('admin/template_main', $data);
	}
	function promotion_create_proc()
	{
		
		if(isset($_POST["create"]))
		{
			$info= array("vi" => htmlentities($this->input->post("detailed_content_vi"), ENT_QUOTES, 'UTF-8'),
		//	"cn" => htmlentities($this->input->post("detailed_content_cn"), ENT_QUOTES, 'UTF-8'),
			"en" => htmlentities($this->input->post("detailed_content_en"), ENT_QUOTES, 'UTF-8'),);
			$name = array("vi" => htmlentities($this->input->post("title_vi"), ENT_QUOTES, 'UTF-8'),
		//	"cn" => htmlentities($this->input->post("title_cn"), ENT_QUOTES, 'UTF-8'),
			"en" => htmlentities($this->input->post("title_en"), ENT_QUOTES, 'UTF-8'),);
			
			
			$data = array(
			"name" =>serialize($name),
			"content" => serialize($info),
			
			
			);
	
	
			
		if($this->Mpromotion->insert($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
	
	
	}

	
	
	
	// search 
	
	 function search()

	{

		$keyword = $this->input->post('keyword');

		if ( ! empty($keyword))

		{

			$data['keyword'] = $keyword;

      $data['search_type'] = $this->input->post('search_type');

			$data['result'] = $this->Muser->search();



      //reload template

			$data['main'] = 'user_search';

      $data['title'] = $this->lang->line('title_promotion_search');

			$this->load->view('admin/template_main', $data);

		}

		else

		{

			redirect('admin/user/review', 'refresh');

		}

	}

	
	
	
	function request()

	{

		//determine 'action'
	 

		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{

			if (in_array($key, $require))

			{

				$act = $key;
				
			}

		}



		//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Mpromotion->delete();

				} break;

			}//end switch



			redirect('admin/promotion/', 'refresh');

		}//end if

	}
	
}









?>
